package loadvideo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.weipiao.R;
import com.example.administrator.weipiao.VideoActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/9/11.
 */
public class VideoAdapter extends BaseAdapter {
    private List<Video> mVideo=new ArrayList<>();
    private Context context;
    public void setData(List<Video> data) {
        this.mVideo = data;
    }
    public VideoAdapter(Context context){
        this.context = context;
    }
    @Override
    public int getCount() {
        return mVideo!=null?mVideo.size():0;
    }

    @Override
    public Object getItem(int position) {
        return mVideo!=null?mVideo.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_video,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Video video = mVideo.get(position);
//        holder.ivLogo.setText(video.getGrade());
//        holder.ivLogo.setImageBitmap(video.setLogo(););
        ImageTask task=new ImageTask(holder.ivLogo);
        task.execute(video.getLogo());
        holder.tvName.setText(video.getName());
        holder.tvHighlight.setText(video.getHighlight());
        holder.tvGrade.setText(video.getGrade());
        holder.btMobilepreview.setText(video.getMobilePreview());
        //跳转页面
        holder.btMobilepreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("movie", (String) holder.btMobilepreview.getText());
                Intent intent = new Intent(context,VideoActivity.class);
                intent.putExtras(bundle);
                context.startActivity(intent);
//                context.startActivity(new Intent(getCount(),VideoActivity.class);
            }
        });

        return convertView;
    }
    class ViewHolder{
        private ImageView ivLogo;
        private TextView tvName,tvHighlight,tvGrade;
        private Button btMobilepreview;
        public ViewHolder(View view){
            ivLogo = (ImageView) view.findViewById(R.id.find_logo);
            tvName = (TextView) view.findViewById(R.id.find_name);
            tvHighlight = (TextView) view.findViewById(R.id.find_highlight);
            tvGrade = (TextView) view.findViewById(R.id.find_grade);
            btMobilepreview = (Button) view.findViewById(R.id.button);
        }
    }
}
